package com.ctgu.tengyun.manager;

import com.ctgu.tengyun.bean.errorcode.PermissionErrorCode;
import com.ctgu.tengyun.bean.response.RolePermission;
import com.ctgu.tengyun.config.DataSourcEelectricConfig;
import com.ctgu.tengyun.domain.PermissionResource;
import com.ctgu.tengyun.domain.PermissionRole;
import com.ctgu.tengyun.domain.SysRole;
import com.ctgu.tengyun.domain.SysUser;
import com.ctgu.tengyun.exception.ErrorCodeException;
import com.ctgu.tengyun.service.permission.PermissionResourceService;
import com.ctgu.tengyun.service.permission.PermissionRoleService;
import com.ctgu.tengyun.service.role.SysRoleService;
import com.ctgu.tengyun.service.user.SysUserService;
import com.ctgu.tengyun.util.CollectionsUtil;
import com.ctgu.tengyun.util.Validator;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Created by liaoweizhi on 2019/8/15.
 */
@Component
public class PermissionManager {

    @Resource
    private PermissionResourceService permissionResourceService;
    @Resource
    private PermissionRoleService permissionRoleService;
    @Resource
    private SysUserService sysUserService;
    @Resource
    private SysRoleService sysRoleService;

//    public List<PermissionResource> getPermissionInfoList(Long userId) {
//        SysUser sysUser = sysUserService.selectByPrimaryKey(userId);
    public List<PermissionResource> getPermissionInfoListByRoleId(Integer roleId) {
        List<PermissionRole> permissionRoleList = permissionRoleService.findByRoleId(roleId);

        Set<Integer> resourceIds = new HashSet<>(permissionRoleList.size());
        permissionRoleList.forEach(item -> {
            resourceIds.add(item.getResourceId());
        });

        return permissionResourceService.findByResourceIds(resourceIds);
    }

    public List<PermissionResource> getPermissionInfoListByUserId(Long userId) {
        SysUser sysUser = sysUserService.selectByPrimaryKey(userId);
        List<PermissionRole> permissionRoleList = permissionRoleService.findByRoleId(sysUser.getRoleId());

        Set<Integer> resourceIds = new HashSet<>(permissionRoleList.size());
        permissionRoleList.forEach(item -> {
            resourceIds.add(item.getResourceId());
        });

        return permissionResourceService.findByResourceIds(resourceIds);
    }

    private void checkPermission(RolePermission rolePermission){
        Integer roleId = rolePermission.getRoleId();
        SysRole sysRole = sysRoleService.selectByPrimaryKey(roleId);
        Validator.assertResourceFound(null != sysRole, "role not found,roleId:" + roleId);

        List<PermissionResource> rolePermissionList = rolePermission.getRolePermissionList();
        if(!CollectionsUtil.isNullOrEmpty(rolePermissionList)){
            Set<Integer> resourceIds = CollectionsUtil.getComposerIdSet(rolePermissionList, PermissionResource.ID_MAP_COMPOSER);
            List<PermissionResource> permissionResourceList = permissionResourceService.findByResourceIds(resourceIds);
            if(resourceIds.size() != permissionResourceList.size()){
                throw new ErrorCodeException(PermissionErrorCode.invalid_resource_id, PermissionErrorCode.invalid_resource_id.getMsg());
            }
        }
    }

    @Transactional(value = DataSourcEelectricConfig.TX_MANAGER)
    public Integer upsert(RolePermission rolePermission){
        checkPermission(rolePermission);
        return permissionRoleService.upsert(rolePermission);
    }

}
